package wang.jinggo.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;


public class DateUtil {
	// 标准的中文日期格式
	private static SimpleDateFormat fdate = new SimpleDateFormat("yyyy-MM-dd");
	private static SimpleDateFormat ftime = new SimpleDateFormat("HH:mm:ss");
	private static SimpleDateFormat fdatetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	private static String datespliter = "-";// 日期分隔符
	private static String timespliter = ":";// 时间分隔符
	private static String datetimespliter = " ";// 日期和时间分隔符

	/**
	 * 输出标准日期
	 */
	public static String getDateStr(long millionSecond) {
		return fdate.format(new Date(millionSecond));
	}

	/**
	 * 输出标准时间
	 *
	 * @param millionSecond
	 * @return
	 */
	public static String getTimeStr(long millionSecond) {
		return ftime.format(new Date(millionSecond));
	}

	/**
	 * 输出标准日期时间
	 *
	 * @param millionSecond
	 * @return
	 */
	public static String getDateTimeStr(long millionSecond) {
		return fdatetime.format(new Date(millionSecond));
	}

	/**
	 * 自定义时间格式输出 (e.g. yyyy-MM-dd HH:mm:ss.f)
	 *
	 * @param millionSecond
	 * @param pattern
	 * @return
	 */
	public static String date2Str(long millionSecond, String pattern) {
		return new SimpleDateFormat(pattern)
				.format(new Date(millionSecond));
	}

	/**
	 * 格式化日期
	 *
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String format(Date date, String pattern) {
		if (fdate.toPattern().equals(pattern)) {
			return fdate.format(date);
		} else if (fdatetime.toPattern().equals(pattern)) {
			return fdatetime.format(date);
		} else if (ftime.toPattern().equals(pattern)) {
			return ftime.format(date);
		} else {
			return new SimpleDateFormat(pattern).format(date);
		}
	}

	/**
	 * 格式化成长日期格式
	 *
	 * @param date
	 * @return
	 */
	public static String format2DateTime(Date date) {
		return fdatetime.format(date);
	}

	/**
	 * 日期时间通用转换，可能转换(19)99-12-1 或 12:23 或 99-12-1 12:23(:34) 的格式
	 *
	 * @param dateStr
	 * @return
	 * @throws ParseException
	 */
	public static Calendar parse(String dateStr) throws ParseException {
		String[] parts = dateStr.split(datetimespliter);
		Calendar c = Calendar.getInstance();
		if (parts.length > 1) {
			// 完整的日期时间字段
			parseDate(c, parts[0]);
			parseTime(c, parts[1]);
		} else {
			if (parts[0].indexOf(datespliter) != -1) {
				parseDate(c, parts[0]);
				parseTime(c, "00:00:00");
			} else if (parts[0].indexOf(timespliter) != -1) {
				parseTime(c, parts[0]);
			} else {
				throw new ParseException("转换错误，未知的日期格式" + dateStr, 0);
			}

		}
		return c;
	}

	protected static void parseDate(Calendar c, String dateStr) {
		String[] dateparts = dateStr.split(datespliter);
		int year = Integer.parseInt(dateparts[0]);
		if (year < 100) {
			year += 1900;
		}
		c.set(Calendar.YEAR, year);
		c.set(c.MONTH, Integer.parseInt(dateparts[1]) - 1);
		c.set(c.DAY_OF_MONTH, Integer.parseInt(dateparts[2]));
	}

	protected static void parseTime(Calendar c, String timeStr) {
		String[] timeparts = timeStr.split("\\" + timespliter);
		c.set(c.HOUR_OF_DAY, Integer.parseInt(timeparts[0]));
		c.set(c.MINUTE, Integer.parseInt(timeparts[1]));
		if (timeparts.length > 2) {
			c.set(c.SECOND, Integer.parseInt(timeparts[2]));
		} else {
			c.set(c.SECOND, 0);
		}
	}

	/**
	 * 转换字符串至日期
	 *
	 * @param sdate
	 * @return
	 * @throws ParseException
	 */
	public static Date parse2date(String sdate) throws ParseException {
		Calendar calendar = parse(sdate);
		if (calendar != null) {
			return calendar.getTime();
		}
		return null;
	}

	/**
	 * 字符串转Timestamp
	 * @param date 字符串
	 * @return
	 * @throws ParseException
	 */
	public static Timestamp parse2Timestamp(String date) throws ParseException{
		Timestamp ts = new Timestamp(System.currentTimeMillis());
		ts = Timestamp.valueOf(date);
		return ts;
	}

	/**
	 * 根据指定的格式转换数据
	 *
	 * @param sdate
	 * @param pattern
	 * @return
	 * @throws ParseException
	 */
	public static Date parse2date(String sdate, String pattern)
			throws ParseException {
		return new SimpleDateFormat(pattern).parse(sdate);
	}

	public static Date parseDate(String dateStr) {

		DateFormat dateFormat = null;

		if (dateStr.indexOf("CST") >= 0) {
			dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy",
					Locale.US);
		} else if (dateStr.indexOf("CDT") >= 0) {
			dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy",
					Locale.US);
		} else if (dateStr.indexOf("UTC") >= 0) {
			dateFormat = new SimpleDateFormat(
					"EEE MMM dd HH:mm:ss 'UTC'Z yyyy", Locale.US);
		} else if (dateStr.indexOf("-") < 0 && dateStr.length() == 8) {
			dateFormat = new SimpleDateFormat("yyyyMMdd");
		} else {
			dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		}

		try {
			return dateFormat.parse(dateStr);
		} catch (ParseException e) {

		}
		return null;
	}

	/**
	 * 返回 多久前 的时间
	 *
	 * @param begin
	 * @return
	 */
	public static String formatTimeAgo(long begin) {
		long ms = System.currentTimeMillis() - begin;
		int second = (int) (ms / 1000);
		int minute = 0;
		int hour = 0;
		int day = 0;
		//int week = 0;
		//int month = 0;
		String ct = "刚刚";
		if (second > 60) {
			minute = second / 60;
			ct = minute + "分钟前";
			if (minute > 60) {
				hour = minute / 60;
				ct = hour + "小时前";
				if (hour > 24) {
					day = hour / 24;
					ct = day + "天前";
					if (day > 7) {
						SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
						ct = sdf.format(new Date(begin));
						if (day > 365) {
							sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
							ct = sdf.format(new Date(begin));
						}
					}
				}
			}
		}

		return ct;
	}

	public static void main(String[] args) {
		try {
			Timestamp t = new Timestamp(System.currentTimeMillis());
			String date = "2018-06-22 10:10:10";
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date d = df.parse(date);
			System.out.println(formatTimeAgo(d.getTime()));
		} catch (Exception e) {

		}
	}

	public static String formatTimeYMDHMS(Date date) {
		DateFormat format1 = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		return format1.format(date);
	}


	public static String formatTimeYMDH(Date date) {
		DateFormat format1 = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm");
		return format1.format(date);
	}

	/**
	 * 根据日期字符串返回java.sql.Date
	 * @param strDate 日期字符串
	 * @return java.sql.Date
	 */
	public static Date strToDate(String strDate){
		Date date = parseDate(strDate);
		return new Date(date.getTime());
	}
}
